package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDto;
import com.sky.dto.CategoryQueryPageDto;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.exception.extend.StatusIsEnableException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.ICategoryService;
import com.sky.service.IDishService;
import com.sky.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;
    @Override
    public PageResult<Category> queryPage(CategoryQueryPageDto categoryQueryPageDto) {
        PageHelper.startPage(categoryQueryPageDto.getPage(), categoryQueryPageDto.getPageSize());
       Page<Category> page = categoryMapper.queryPage(categoryQueryPageDto);
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    @Override
    public void insert(CategoryDto categoryDto) {
        //先将传来的数据封装成数据库实体类
        Category category=new Category();
        BeanUtils.copyProperties(categoryDto,category);
        category.setStatus(1);
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> listByType(String type) {
        return categoryMapper.findListByType(type);
    }

    @Override
    public void modifyStatus(String status, String id) {
        //先进行判断
        if(Integer.parseInt(status)==0){
            //如果要修改的状态为0，那么判断其他关联表有没有数据，如果有，那么抛出业务异常
            //已知数据库与分类关联的表有 菜品 套餐
            int dishNumber=dishService.countDishes(Long.valueOf(id));
            int setmealNumber=setmealService.countSetmeals(Long.valueOf(id));
            if(dishNumber!=0 || setmealNumber!=0){
                throw new BusinessException("该分类关联了菜品或者套餐，不能修改或者删除");
            }
        }

        //进行数据封装
        Category category = Category.builder()
                .id(Long.valueOf(id))
                .status(Integer.valueOf(status))
                .build();
        // 再传入持久层
        categoryMapper.modifyStatus(category);
    }

    @Override
    public void modify(CategoryDto categoryDto) {
        //先封装
        Category category=new Category();
        BeanUtils.copyProperties(categoryDto,category);
        categoryMapper.updateCategory(category);
    }

    @Override
    public void removeById(Long id) {
        int dishNumber=dishService.countDishes(id);
        int setmealNumber=setmealService.countSetmeals(id);
        if(dishNumber!=0 || setmealNumber!=0){
            throw new BusinessException("该分类关联了菜品或者套餐，不能修改或者删除");
        }
        //1 先更具id的值查找当前id对应的status
        Integer status=categoryMapper.findById(id);
        if(status==1){
            throw new StatusIsEnableException(MessageConstant.STATUS_IS_ENABLE);
        }else {
            //status=0
            //则开始删除
            categoryMapper.remove(id);
        }
    }

    @Override
    public List<Category> listCategory() {
       List<Category> categoryList= categoryMapper.list();
        return categoryList;
    }
}
